<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OfficialAccountMedia.php 2018-05-24 17:30 likexin
   
*/

namespace common\library\wechat\officialaccount;

use common\component\wechat\Wechat;
use common\library\wechat\WechatApi;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;

class OfficialAccountMedia extends WechatApi
{

    /**
     * 上传图片
     * @param string $path 图片路径
     * @return array|bool|void
     * @others : 2M, 支持JPG格式
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738726
     */
    public static function uploadImage(string $path)
    {
        try {
            $result = self::getInstance()->media->uploadImage($path);
        } catch (InvalidArgumentException $exception){
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

    /**
     * 上传音频
     * @param string $path 音频路径
     * @return array|bool|void
     * @others 2M, 播放长度不超过60s, 支持AMR\MP3格式
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738726
     */
    public static function uploadVoice(string $path)
    {
        try {
            $result = self::getInstance()->media->uploadVoice($path);
        } catch (InvalidArgumentException $exception){
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

    /**
     * 上传视频
     * @param string $path 视频路径
     * @return array|bool|void
     * @others 10MB, 支持MP4格式
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738726
     */
    public static function uploadVideo(string $path)
    {
        try {
            $result = self::getInstance()->media->uploadVideo($path);
        } catch (InvalidArgumentException $exception){
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

    /**
     * 上传缩略图
     * @param string $path 图片路径
     * @return array|bool|void
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738726
     */
    public static function uploadThumb(string $path)
    {
        try {
            $result = self::getInstance()->media->uploadThumb($path);
        } catch (InvalidArgumentException $exception){
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

    /**
     * 上传群发视频
     * @param string $path 视频路径
     * @param string $title 标题
     * @param string $description 介绍
     * @return array|bool|void
     * @others ,
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738726
     */
    public static function uploadVideoForBroadcasting(string $path, string $title, string $description)
    {
        try {
            $result = self::getInstance()->media->uploadVideoForBroadcasting($path, $title, $description);
        } catch (InvalidArgumentException $exception){
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

    /**
     * 创建群发消息
     * @param string $mediaId 素材ID
     * @param string $title 标题
     * @param string $description 介绍
     * @return array|bool|void
     * @others 使用该 media_id 加标题描述 创建一条消息素材 用来发送给用户
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     */
    public static function createVideoForBroadcasting(string $mediaId, string $title, string $description)
    {
        try {
            $result = self::getInstance()->media->createVideoForBroadcasting($mediaId, $title, $description);
        } catch (InvalidArgumentException $exception){
            return error($exception->getMessage());
        }
        return Wechat::apiError($result);
    }

    /**
     * 获取临时素材内容
     * @param string $mediaId 素材ID
     * @return array|bool|void [save, saveAs]
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738727
     */
    public static function get(string $mediaId)
    {
        $result = self::getInstance()->media->get($mediaId);
        return Wechat::apiError($result);
    }

    /**
     * 获取 JSSDK 上传的高清语音
     * @param string $mediaId 素材ID
     * @return array|bool|void [saveAs]
     * @link https://www.easywechat.com/docs/master/zh-CN/basic-services/media
     * @link https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738727
     */
    public static function getJssdkMedia(string $mediaId)
    {
        $result = self::getInstance()->media->getJssdkMedia($mediaId);
        return Wechat::apiError($result);
    }

}